package controllers;

import beans.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.impl.OrganServicesImpl;
import services.impl.ProductServicesImpl;
import services.impl.ProfileServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductServicesImpl psi;

    @RequestMapping("/product")
    public List product(){
        List<Product> list = psi.selectList();
        return list;
    }

    @RequestMapping("/createproduct")
    public String createProduct(@RequestBody Product product){
        psi.insertSelective(product);
        return null;
    }

    @RequestMapping("/editproduct")
    public String editProduct(@RequestBody Product product){
        psi.updateByPrimaryKeySelective(product);
        return null;
    }

    @RequestMapping("/detailsproduct")
    public Product detailsProduct(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("productId"));
        Product product = psi.selectByPrimaryKey(i);
        return product;
    }

    @RequestMapping("/searchproduct")
    public List<Product> searchProduct(@RequestBody Map<String,String> request){
        String select = request.get("selectText");
        String input = request.get("inputText");
        List<Product> list = psi.selectSelective(select,input);
        return list;
    }

    @RequestMapping("/deleteproduct")
    public String deleteProduct(HttpServletRequest request){
        int i = Integer.parseInt(request.getParameter("productId"));
        psi.deleteByPrimaryKey(i);
        return null;
    }
}
